package main.java.com.async.middleware;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javafx.scene.control.Alert;
import javafx.scene.control.ScrollPane;
import javafx.scene.paint.Paint;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import main.component.infodialog.InfoDialogControl;

public class JavaFxControllerMiddleware {
	
	private static SimpleDateFormat _simpleDateFormat;
	private static ArrayList<CreoInfoCache> _creoInfo = new ArrayList<CreoInfoCache>();
	
	
	public static void infoToUser(TextFlow textFlowInfo, ScrollPane scrollPaneInfo, String infoConten, String colorCode) {
//		默认为黑色字体
		if(colorCode == null) {
			colorCode = "#000000";
		}
		else if(colorCode == "red") {
			colorCode = "#ff0000";
		}
//		初始化时间格式
		_simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
		String timeNow = _simpleDateFormat.format(new Date());
		Text contentText = new Text();
		contentText.setText("[" + timeNow + "] " + infoConten + "\n");
		contentText.setFill(Paint.valueOf(colorCode));
		textFlowInfo.getChildren().add(contentText);
//		有信息显示时滚动条位于最下方
		scrollPaneInfo.setVvalue(1.0);
	}

	/**
	 * 用于缓存Creo业务代码的通知信息
	 * @param content 信息内容
	 * @param colorCode 信息字体颜色
	 */
	public static void cacheCreoInfo(String content, String colorCode){
		_creoInfo.add(new CreoInfoCache(content, colorCode));
	}

	/**
	 * 用于显示Creo业务代码的通知信息
	 * @param textFlowInfo textflow控件对象
	 * @param scrollPaneInfo	与textflow控件匹配的scrollPane控件对象
	 */
	public static void displayCreoInfo(TextFlow textFlowInfo, ScrollPane scrollPaneInfo){
		if(_creoInfo.size() != 0){
			for (CreoInfoCache creoInfoCache:_creoInfo) {
				infoToUser(textFlowInfo, scrollPaneInfo, creoInfoCache.getCreoInfoContent(),
						creoInfoCache.getCreoContentColorCode());
			}
			_creoInfo.clear();
		}
	}
}

//为缓存AsyncWoodTookit的信息单独设计的数据结构
class CreoInfoCache{

	private String _content;
	private String _colorCode;

	public CreoInfoCache(String content, String colorCode){
		_content = content;
		_colorCode = colorCode;
	}

	public String getCreoInfoContent(){
		return _content;
	}

	public String getCreoContentColorCode(){
		return _colorCode;
	}
}
